<template>
    <div>

      <van-field label="手机号" placeholder="请输入手机号" v-model="form.mobile"></van-field>
      <van-button @click="getcode">获取验证码</van-button>
      <van-field label="验证码" placeholder="请输入验证码" v-model="form.smscode" @blur="panduan"></van-field>
      <van-field label="密码" placeholder="请输入密码" v-model="form.password"></van-field>
      <van-field label="确认密码" placeholder="请再次输入密码" v-model="form.password2" @blur="passwordpanduan"></van-field>
      <van-button @click="add">注册</van-button>


    </div>
</template>

<script>
    export default {
        data(){
          return{
            form:{},
            smscode:''
          }
        },
        methods: {
          //获取验证码
          getcode(){
            //判断手机号合法
            let res =  /^1[3-9][0-9]{9}$/

            if(!res.test(this.form.mobile)){
              alert('手机号不合法')
              return
            }
            this.$axios.get('/smscode/?mobile='+this.form.mobile).then(res=>{
              if(res.data.code == 200){
                console.log(res.data)
                alert(res.data.smscode)
                this.smscode = res.data.smscode
              }
            
            })
          },
          //密码判断
          passwordpanduan(){
            if(this.form.password != this.form.password2){
              alert('密码错误')
            }
              
            
          },
          //判断code和redis的是否一样
          panduan(){
            if(this.smscode != this.form.smscode){
              alert('验证码输入错误')
            }
          },
           //注册
          add(){
            this.$axios.post('/userM/',this.form).then(res=>{
              alert('注册成功')
              this.$router.push('/day15login')
            })
          }
        },
      
    }
</script>

<style scoped>

</style>
